<?php
namespace hdphp\user;

/**
* 用户管理服务
*/
class User
{
	public function isLogin()
	{
		if (!Session::get('uid')) {
			message('请先登录','admin/entry/login','warning');
		}
	}



	//用户登录服务
	public function login()
	{
		Validate::make( [
			[ 'username', 'required', '帐号不能为空' ],
			[ 'password', 'required', '密码不能为空' ]
		] );
		if (Validate::fail()) {
			message(Validate::getError(),'','error');
		}
		$db = new \system\model\User();
		$user=$db->where('username',Request::post('username'))->first();
		if (!$user) {
			message('账号不存在','','error');
		}
		if ($user['password'] != md5(Request::post('password'))) {
			message('密码错误','','error');
		}
		$db->uid=$user['uid'];
		// $db->username=$user['username'];
		// $db->password=$user['password'];
		$db->loginip=Request::ip();
		$db->logintime=time();
		$db->save();
		Session::set('uid',$user['uid']);
		return TRUE;
	}

	public function quit()
	{
		if (Session::get('uid')) {
			Session::del('uid');
			return TRUE;
		}
		return false;
	}
}